
layui.config({
    base: '/html/myMoudle/' //假设这是你存放拓展模块的根目录
}).extend({
    admin: 'admin' //核心模块
    ,view: 'view' //核心模块
    ,index:'index'
    ,echarts:'echarts'
    ,echartsTheme:'echartsTheme'
    ,console:'console'
    ,inv:'inv'
});
layui.define(function(exports){


    layui.use(['admin', 'carousel'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,carousel = layui.carousel
            ,element = layui.element
            ,device = layui.device();

        //轮播切换
        $('.layadmin-carousel').each(function(){
            var othis = $(this);
            carousel.render({
                elem: this
                ,width: '100%'
                ,arrow: 'none'
                ,interval: othis.data('interval')
                ,autoplay: othis.data('autoplay') === true
                ,trigger: (device.ios || device.android) ? 'click' : 'hover'
                ,anim: othis.data('anim')
            });
        });

        element.render('progress');

    });
    /*layui.use('layer', function() {
        var $ = layui.$ ;
        var layer=layui.layer;//由于layer弹层依赖jQuery，所以可以直接得到

    });*/
    //数据概览
    layui.use(['admin', 'carousel', 'echarts'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,carousel = layui.carousel
            ,echarts = layui.echarts;
        var Arr = new Array();//名称
        var ArrNum = new Array()//个数
        var Arr1 = [];//名称
        var ArrNum1 = []//个数
        var Arr2 = [];//名称
        var ArrNum2 = []//个数
        var dataIntArr1=[];//保存转换后的整型字符串
        var name1=[];//保存转换后的整型字符串

        $(document).ready(function () {

            $.ajax({
                url: '/hospital/sale/shan',
                dataType: 'json',
                type: 'get',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                    // $.each(data.data, function (index, item) {
                    if (data.data != null) {
                        var obj = eval(data.data);//转换为JSON对象
                        for (var i = 0; i < obj.length; i++) {

                            Arr.push(obj[i].name);//存入arr
                            ArrNum.push({ value: obj[i].count,name: obj[i].name});//存入arrnum</span>
                            console.log(Arr);
                            console.log(ArrNum);
                        }

                    }
                    // })
                }

            })
            $.ajax({
                url: '/hospital/sale/zhe',
                dataType: 'json',
                type: 'get',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                    // $.each(data.data, function (index, item) {
                    if (data.data != null) {
                        var obj = eval(data.data);//转换为JSON对象
                        for (var i = 0; i < obj.length; i++) {

                            Arr2.push(obj[i].date);//存入arr
                            ArrNum2.push(obj[i].total);

                        }
                        // console.log(Arr);

                    }
                    // })
                }

            })
            $.ajax({
                url: '/hospital/inv/zhi',
                dataType: 'json',
                type: 'get',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.data.length != 0) {
                        var obj = eval(data.data);//转换为JSON对象
                        for (var i = 0; i < obj.length; i++) {

                            Arr1.push(obj[i].name);//存入arr
                            ArrNum1.push(obj[i].count);//存入arrnum</span>

                        }

                    }

                    dataIntArr1 = ArrNum1.map(function (data) {
                        return +data;
                    });
                    for (j = 0, len = Arr1.length; j < len; j++) {
                        name1[j] = Arr1[j];
                    }
                }
            })
            var echartsApp = [], options = [
                {
                    title : {
                        text: '今日出库药品分布',
                        x: 'center',
                        textStyle: {
                            fontSize: 14
                        }
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        x : 'center',
                        y : 'bottom',
                        data:Arr
                    },
                    series : [
                        {
                            name:'药品名称',
                            type:'pie',
                            radius : [30, 110],
                            roseType : 'area',
                            x: '100%',               // for funnel
                            max: 40,                // for funnel
                            sort : 'ascending',     // for funnel
                            data:ArrNum
                        }
                    ]
                }, {
                    title : {
                        text: '本月销售额',
                        x: 'left',
                        textStyle: {
                            fontSize: 14
                        }
                    },
                    tooltip : {
                        trigger: 'axis'
                    },
                    legend: {
                        data:['销售额']
                    },
                    xAxis : [
                        {
                            type : 'category',
                            boundaryGap : false,
                            data : Arr2
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            name:'销售额',
                            type:'line',
                            data:ArrNum2/*,
                            markPoint : {
                                data : [
                                    {type : 'max', name: '最大值'},
                                    {type : 'min', name: '最小值'}
                                ]
                            },
                            markLine : {
                                data : [
                                    {type : 'average', name : '平均值'}
                                ]
                            }*/
                        }
                    ]
                },{
                    // title : {
                    //     text: '药品库存量',
                    //     x: 'center',
                    //     textStyle: {
                    //         fontSize: 14
                    //     }
                    // },
                    tooltip : {
                        trigger: 'axis'
                    },
                    legend: {
                        data:['库存量']
                    },


                    xAxis : [
                        {
                            type : 'category',
                            data : name1,
                            axisLabel: {
                                interval: 0,
                                formatter:function(value)
                                {
                                    return value.split("").join("\n");
                                }
                            }
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            name:'库存量',
                            type:'bar',
                            data:ArrNum1

                        }
                    ]
                }
            ]
                ,elemDataView = $('#LAY-index-dataview').children('div')
            console.log("dataIntArr1："+dataIntArr1);
            renderDataView = function(index){
                echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
                echartsApp[index].setOption(options[index]);
                //window.onresize = echartsApp[index].resize;
                // admin.resize(function(){
                //     echartsApp[index].resize();
                // });
            };


            //没找到DOM，终止执行
            if(!elemDataView[0]) return;

            renderDataView(0);

        });


        //监听数据概览轮播
        var carouselIndex = 0;
        carousel.on('change(LAY-index-dataview)', function(obj){
            renderDataView(carouselIndex = obj.index);
        });

        //监听侧边伸缩
        layui.admin.on('side', function(){
            setTimeout(function(){
                renderDataView(carouselIndex);
            }, 300);
        });

        //监听路由
        layui.admin.on('hash(tab)', function(){
            layui.router().path.join('') || renderDataView(carouselIndex);
        });
    });


    //最新订单
    layui.use('table', function(){
        var $ = layui.$
            ,table = layui.table;

     /*   //今日热搜
        table.render({
            elem: '#LAY-index-topSearch'
            ,url: '/hospital/sale/all' //模拟接口
            ,contentType: "application/json; charset=utf-8"
            ,page: false
            ,cols: [[
                {type: 'numbers', fixed: 'left'}
                ,{field: 'pat_name', title: '姓名', minWidth: 20, templet: '<div><a href="https://www.baidu.com/s?wd={{ d.keywords }}" target="_blank" class="layui-table-link">{{ d.keywords }}</div>'}
                ,{field: 'mdc_name', title: '药品', minWidth: 30, sort: true}
                ,{field: 'count', title: '数量', sort: true}
            ]]
            ,skin: 'line'
        });

        //今日热贴
        table.render({
            elem: '#LAY-index-topCard'
            ,url: './json/console/top-card.js' //模拟接口
            ,page: true
            ,cellMinWidth: 120
            ,cols: [[
                {type: 'numbers', fixed: 'left'}
                ,{field: 'title', title: '标题', minWidth: 300, templet: '<div><a href="{{ d.href }}" target="_blank" class="layui-table-link">{{ d.title }}</div>'}
                ,{field: 'username', title: '发帖者'}
                ,{field: 'channel', title: '类别'}
                ,{field: 'crt', title: '点击率', sort: true}
            ]]
            ,skin: 'line'
        });*/
    });

    exports('console', {})
});